
'''
    属性描述符
    
'''


class ReadOnly(object):
    def __init__(self, name):
        self.name = name

    def __get__(self, instance, owner):
        return instance.__dict__[self.name]

    def __set__(self, instance, value):
        if self.name in instance.__dict__:
            raise ValueError(f'{self.name} is readonly') 
        else:
            instance.__dict__[self.name] = value

 
class Person():
    birth = ReadOnly('birth')
    

p = Person()
p.birth = '1919-02-11'
print(p.__dict__)

p.birth = '1919-02-12'   # 报错 ！！